﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using Microsoft.SharePoint;
using System.Collections;
using Microsoft.SharePoint.UserCode;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Administration;

namespace CreateSiteActivity
{

    public sealed class CreateSiteAction : CodeActivity
    {
        // Define an activity input argument of type string
        public InArgument<string> SiteName { get; set; }

        public InArgument<string> URL { get; set; }

        // If your activity returns a value, derive from CodeActivity<TResult>
        // and return the value from the Execute method.
        protected override void Execute(CodeActivityContext context)
        {
            // Obtain the runtime value of the Text input argument
            string siteName = context.GetValue(this.SiteName);
            string url = context.GetValue(this.URL);
            
            try
            {
                using (SPSite site = new SPSite(url))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        web.Webs.Add(
                            siteName,
                            "New site: " + siteName,
                            string.Empty,
                            1033,
                            "STS",
                            false,
                            false);
                    }
                }
            }
            catch(Exception ex)
            {
                SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("SP Custom Activity", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, ex.Message, ex.StackTrace);
            }

        }
    }
}
